package com.kk.base.controller.home;


import com.kk.base.bean.CodeMsg;
import com.kk.base.bean.PageBean;
import com.kk.base.bean.Result;
import com.kk.base.entity.home.BranchApplyFor;
import com.kk.base.entity.home.BranchApplyForStatusEnum;
import com.kk.base.entity.home.BranchTypeEnum;
import com.kk.base.entity.home.HomeUser;
import com.kk.base.service.admin.UserService;
import com.kk.base.service.home.BranchApplyForService;
import com.kk.base.util.SessionUtil;
import com.kk.base.util.ValidateEntityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 网点申请控制层
 */
@RequestMapping("/home/branch")
@Controller
public class HomeBranchApplyForController {


    @Autowired
    private BranchApplyForService branchApplyForService;
    @Autowired
    private UserService userService;


    /**
     * 跳转到申请页面
     * @param model
     * @return
     */
    @RequestMapping("/index")
    public String index(Model model, PageBean<BranchApplyFor> pageBean) {
        HomeUser homeUser = SessionUtil.getHomeUser();
        model.addAttribute("pageBean", branchApplyForService.findList(pageBean, null, homeUser.getId()));
        model.addAttribute("index", 6);
        model.addAttribute("branchTypes", BranchTypeEnum.values());
        return "home/branch/index";
    }


    /**
     * 网点申请保存
     * @param branchApplyFor
     * @return
     */
    @RequestMapping(value = "/applyFor", method = RequestMethod.POST)
    @ResponseBody
    public Result<Boolean> applyFor(BranchApplyFor branchApplyFor) {
        HomeUser homeUser = SessionUtil.getHomeUser();
        BranchApplyFor forStatus = branchApplyForService.findByHomeUser_IdAndBranchApplyForStatus(
                homeUser.getId(), BranchApplyForStatusEnum.VERIFYTHE);
        if (forStatus != null){
            return Result.error(CodeMsg.BRANCHAPPLYFOR_EXIST);
        }
        CodeMsg validate = ValidateEntityUtil.validate(branchApplyFor);
        if (validate.getCode() != CodeMsg.SUCCESS.getCode()) {
            return Result.error(validate);
        }
        //判断网点申请名是否存在
        if (branchApplyForService.isExistBranchName(branchApplyFor.getAdminName(), 0L)) {
            return Result.error(CodeMsg.BRANCHES_USERNAME_EXIST);
        }
        if (userService.isExistUsername(branchApplyFor.getBranchName(),0L)){
            return Result.error(CodeMsg.BRANCHES_USERNAME_EXIST);
        }
        branchApplyFor.setHomeUser(homeUser);
        branchApplyFor.setBranchApplyForStatus(BranchApplyForStatusEnum.VERIFYTHE);
        if (branchApplyForService.save(branchApplyFor) == null) {
            return Result.error(CodeMsg.BRANCHAPPLYFOR_ERROR);
        }
        return Result.success(true);
    }


    /**
     * 删除网点申请
     * @param id
     * @return
     */
    @RequestMapping(value = "/deleteApplyFor", method = RequestMethod.POST)
    @ResponseBody
    public Result<Boolean> deleteApplyFor(@RequestParam(name = "id", required = false) Long id) {
        BranchApplyFor branchApplyFor = branchApplyForService.find(id);
        if (branchApplyFor.getBranchApplyForStatus().getCode() == 0) {
            return Result.error(CodeMsg.BRANCHAPPLYFOR_STATUS_ERROR);
        }
        if (branchApplyFor.getBranchApplyForStatus().getCode() == 1) {
            return Result.error(CodeMsg.BRANCHAPPLYFOR_STATUSNOT_ERROR);
        }
        try {
            branchApplyForService.delete(id);
        } catch (Exception e) {
            return Result.error(CodeMsg.BRANCHAPPLYFOR_DELETE_ERROR);
        }
        return Result.success(true);

    }

}
